package com.qzyh.esb.egsa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qzyh.esb.egsa.bean.vo.RequirementContentVO;
import com.qzyh.esb.egsa.controller.RequirementController;
import com.qzyh.esb.egsa.entity.Requirement;
import com.qzyh.esb.egsa.entity.RequirementContent;
import com.qzyh.esb.egsa.entity.RoleMenu;
import com.qzyh.esb.egsa.entity.User;
import com.qzyh.esb.egsa.mapper.RequirementContentMapper;
import com.qzyh.esb.egsa.mapper.RequirementMapper;
import com.qzyh.esb.egsa.mapper.UserMapper;
import com.qzyh.esb.egsa.service.IRequirementContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzyh.esb.egsa.utils.BaseContextUtil;
import com.qzyh.esb.egsa.utils.FillUtil;
import com.qzyh.esb.egsa.utils.ResultObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 需求内容表 服务实现类
 * </p>
 *
 * @author whq
 * @since 2023-09-19
 */
@Service
public class RequirementContentServiceImpl extends ServiceImpl<RequirementContentMapper, RequirementContent> implements IRequirementContentService {

    private static final Logger logger = LoggerFactory.getLogger(RequirementContentServiceImpl.class);

    @Autowired
    private RequirementContentMapper requirementContentMapper;

    @Autowired
    private RequirementMapper requirementMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<RequirementContentVO> getList(IPage<RequirementContentVO> page, String requirementName, String requirementNum, String serviceScence, String startDate, String endDate, String status) {
        return requirementContentMapper.selectRequirementContentList(page, requirementName, requirementNum, serviceScence, startDate, endDate, status);
    }

    @Override
    @Transactional
    public ResultObject saveOrUpdateRequirementInfo(RequirementContent requirementContent) {
        requirementContent = (RequirementContent) FillUtil.fill(requirementContent);
        logger.info("填充后的信息：" + requirementContent.toString());
        if (requirementContent.getId() != null && !"".equals(requirementContent.getId())){
            requirementContentMapper.updateById(requirementContent);
        } else {
            requirementContentMapper.insert(requirementContent);
        }
        //查询需求内容新增修改复用删除的数量
        List<Map<String,Object>> list = requirementContentMapper.selectDataByRequirementId(requirementContent.getRequirementId());
        //查询需求信息
        Requirement requirement = requirementMapper.selectById(requirementContent.getRequirementId());

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            if ("新增".equals(map.get("oprt_type"))){
                requirement.setAddedInterfaces(new Integer(String.valueOf(map.get("count"))));
            } else if ("修改".equals(map.get("oprt_type"))){
                requirement.setModifiedInterfaces(new Integer(String.valueOf(map.get("count"))));
            } else if ("复用".equals(map.get("oprt_type"))){
                requirement.setReusedInterfaces(new Integer(String.valueOf(map.get("count"))));
            } else if ("删除".equals(map.get("oprt_type"))){
                requirement.setDeletedInterfaces(new Integer(String.valueOf(map.get("count"))));
            }
        }
        requirementMapper.updateById(requirement);
        return ResultObject.success("保存成功");
    }

    @Override
    public ResultObject<List<Requirement>> getRequirementList(String keywords) {
        QueryWrapper<Requirement> wrapper = Wrappers.query();
        wrapper.select("id,num,name");
        wrapper.like("name",keywords).or().like("num",keywords);
        List<Requirement> list = requirementMapper.selectList(wrapper);
        return ResultObject.success(list);
    }

    @Override
    @Transactional
    public ResultObject changeStatus(String id, String type) {
        //获取当前用户的用户信息
        User user = userMapper.selectById(BaseContextUtil.getUserId());
        //获取需求内容信息
        RequirementContent requirementContent = requirementContentMapper.selectById(id);
        if ("begin".equals(type)){
            //类型为：开发中
            //校验状态是否为未开发
            if (!"0".equals(requirementContent.getStatus())){
                return ResultObject.fail("状态异常，无法操作");
            }
            //校验通过后 修改状态为 1 开发中
            requirementContent.setStatus("1");

        } else if ("finish".equals(type)){
            //类型为：开发完成
            //校验状态是否为未开发、且开发人员是否和当前用户相同
            if (!"1".equals(requirementContent.getStatus())){
                return ResultObject.fail("状态异常，无法操作");
            }
            if (!user.getUserName().equals(requirementContent.getDeveloper())){
                return ResultObject.fail("非本人开发，无法操作");
            }
            //校验通过后 修改状态为 2 开发完成
            requirementContent.setStatus("2");
        }
        requirementContent.setDeveloper(user.getUserName());
        requirementContent.setUpdateTime(LocalDateTime.now());
        requirementContent.setUpdateUserId(BaseContextUtil.getUserId());
        requirementContentMapper.updateById(requirementContent);
        return ResultObject.success("操作成功");
    }


}
